/**
 * ------------------------------------------------------------*
 *          COPYRIGHT (C) 2006 BOCO Inter-Telecom INC          *
 *   CONFIDENTIAL AND PROPRIETARY. ALL RIGHTS RESERVED.        *
 *                                                             *
 *  This work contains confidential business information       *
 *  and intellectual property of BOCO  Inc, Beijing, CN.       *
 *  All rights reserved.                                       *
 * ------------------------------------------------------------*
 *
 *
 */
/**
 *Revision Information:
 *
 *@version 1.0 2006-2-6 release(zhanglei)
 */

package com.boco.webmaster.common.web;

import com.boco.webmaster.base.*;
import com.boco.common.util.except.UserException;
import com.boco.common.util.db.*;
import com.boco.transnms.common.dto.base.IBoQueryContext;

public class DaoHelper {
    private DaoHelper() {
    }

    public static String getOrderSql(IBoQueryContext queryContext, String defaultOrderField){
        return getOrderSql(queryContext, "", defaultOrderField);
    }

    public static String getOrderSql(IBoQueryContext queryContext, String fieldPrefix, String defaultOrderField){
        String sort = "ASC";
        String orderField = defaultOrderField;
        if (queryContext.isOrderDesc()) {
            sort = "DESC";
        }

        if (queryContext.getOrderField().length() > 0) {
            orderField = queryContext.getOrderField();
        }

        queryContext.setOrderField(orderField);

        if(fieldPrefix.length() > 0){
            fieldPrefix += ".";
        }

        return " order by " + fieldPrefix + orderField  + " " + sort + " ";
    }

    public static boolean isNotEmpty(String str){
        return (str != null && str.trim().length() > 0);
    }

    public static String sqlEqualAnd(String fieldName, String fieldValue){
        return sqlEqualAnd(fieldName, fieldValue, false);
    }

    public static String sqlEqualAnd(String fieldName, String fieldValue, boolean isStrType){
        StringBuffer sql = new StringBuffer();
        if(isNotEmpty(fieldValue)){
            sql.append(" and " + fieldName + "=");
            if(isStrType){
                sql.append("'");
            }
            sql.append(fieldValue);
            if(isStrType){
                sql.append("'");
            }
        }
        return sql.toString();
    }
}
